#include <stdio.h>
#include "MenuManager.h"

MenuManager::MenuManager()
{
	head = NULL;
}
MenuManager::MenuManager(Menu * headMenu)
{
	head = new List<Menu>(headMenu, NULL);
}
MenuManager::~MenuManager()
{
	printf("Deleting MenuManager\n");
	List<Menu> * search = head;
	while(search)
	{
		search = search->Remove();
	}
	printf("Done deleting MenuManager\n");
	head = NULL;
}
void MenuManager::Add(Menu * menu)
{
	head = new List<Menu>(menu, head);
}
void MenuManager::Remove(Menu * menu)
{
	List<Menu> * search = head;
	while(search)
	{
		if(search->payload && search->payload == menu)
		{
			if(search == head)
			{
				head = search->next;
			}
			
			search = search->Remove();
		}
		else
			search = search->next;
	}	
}
void MenuManager::Draw()
{
	List<Menu> * search = head;
	while(search)
	{
		if(search->payload)
			search->payload->Draw();
		search = search->next;
	}	
}
void MenuManager::Hide()
{
	List<Menu> * search = head;
	while(search)
	{
		if(search->payload)
		{
			search->payload->Hide();
		}
		search = search->next;
	}	
}
void MenuManager::Show()
{
	List<Menu> * search = head;
	while(search)
	{
		if(search->payload)
		{
			search->payload->Show();
		}
		search = search->next;
	}	
}
void MenuManager::Hide(int index)
{
	List<Menu> * search = head;
	while(search)
	{
		if(search->payload && (search->payload->GetIndex() == index))
		{
			search->payload->Hide();
			return;
		}
		search = search->next;
	}	
}
void MenuManager::Show(int index)
{
	List<Menu> * search = head;
	while(search)
	{
		if(search->payload && (search->payload->GetIndex() == index))
		{
			printf("Showing index %d\n", index);
			search->payload->Show();
			return;
		}
		search = search->next;
	}	
}
bool MenuManager::Click(Vector2D mousePosition)
{
	List<Menu> * search = head;
	while(search)
	{
		if(search->payload)
			if(search->payload->Click(mousePosition) > 0)
				return true;
				
		search = search->next;
	}	
	return false;
}
bool MenuManager::HandleUserInput(unsigned char key, int modifier, bool isDepressed)
{
	List<Menu> * search = head;
	while(search)
	{
		if(search->payload)
			if(search->payload->HandleUserInput(key,modifier,isDepressed))
				return true;
				
		search = search->next;
	}	
	return false;
}
